//
//  FMDBManager.m
//  WGDemo
//
//  Created by T on 14-3-26.
//  Copyright (c) 2014年 PK. All rights reserved.
//

#import "FMDBManager.h"
#import "AppDelegate.h"
static FMDBManager* manager = nil;
@implementation FMDBManager
+ (FMDBManager *)sharedManager{
    if (manager == nil) {
        manager = [[FMDBManager alloc] init];
    }
    return manager;
}
+ (id)allocWithZone:(struct _NSZone *)zone{
    if (manager == nil) {
        manager = [super allocWithZone:zone];
        return manager;
    }
    return nil;
}
- (id)retain{
    return manager;
}
- (id)autorelease{
    return manager;
}
- (oneway void)release{

}
- (id)init{
    if (self = [super init]) {
        NSString* path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/user.sqlite"];
        _db = [[FMDatabase alloc] initWithPath:path];
        BOOL res = [_db open];
        if (res == NO) {
            NSLog(@"打开失败");
            return self;
        }
        
        //创建User表
        res = [_db executeUpdate:@"create table if not exists USER(uid integer primary key autoincrement,name,menpai,workd,mobel,otherd,homed,telfd)"];
        if (res == NO) {
            NSLog(@"创建User表失败");
        }
    }
    return self;
}
//添加一个用户
- (BOOL)addUser:(DingSh*)user{
    //user.name user.menpai user.wugong
    [_db open];
    BOOL res = [_db executeUpdate:@"insert into USER(name,menpai,workd,mobel,otherd,homed,telfd) values(?,?,?,?,?,?,?)",user.name,user.main,user.work,user.mobile,user.other,user.home,user.tel];
    if (res == NO)
    {
        NSLog(@"插入用户失败");
        return NO;
    }
    return YES;
    [_db close];
}
//拿到插入用户的uid
- (int)getUserid{
    [_db open];
    FMResultSet* set = [_db executeQuery:@"select * from USER order by uid desc limit 1"];
    [set next];
    int uid = [set intForColumn:@"uid"];
    return uid;
    [_db close];
}
//得到全部用户
- (NSMutableArray*)getAllUsers{
    [_db open];
    AppDelegate *app =[UIApplication sharedApplication].delegate;
    [app.dataArray removeAllObjects];
    FMResultSet* set = [_db executeQuery:@"select * from USER"];
    while ([set next]) {
       // int uid = [set intForColumn:@"uid"];
        DingSh* user = [[DingSh alloc] init];
        user.name = [set stringForColumn:@"name"];
        user.main = [set stringForColumn:@"menpai"];
        user.work = [set stringForColumn:@"workd"];
        user.mobile = [set stringForColumn:@"mobel"];
        user.other = [set stringForColumn:@"otherd"];
        user.home = [set stringForColumn:@"homed"];
        user.tel = [set stringForColumn:@"telfd"];
        NSArray *lists =[user.name componentsSeparatedByString:@" "];
        if([[lists objectAtIndex:0] isEqualToString:@""])
        {
            user.name =@"联系人";
        }
        else
        {
            NSLog(@"");
        }
        if(user.main==nil)
        {
            user.main=@"";
        }
        if(user.mobile==nil)
        {
            user.mobile=@"";
        }
        if(user.home==nil)
        {
            user.home=@"";
        }
        if(user.work==nil)
        {
            user.work=@"";
        }
        if(user.other==nil)
        {
            user.other=@"";
        }
        if(user.tel==nil)
        {
            user.tel=@"";
        }
        [app.dataArray addObject:user];
        [user release];
    }
    return app.dataArray;
    [_db close];
}
-(void)removeAllUsers
{
    [_db open];
    [_db executeUpdate:@"delete from USER"];
    [_db close];
}
@end
